#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2023/7/16 12:42
# @File           : data_types.py
# @IDE            : PyCharm
# @desc           : 自定义数据类型

"""
自定义数据类型 - 官方文档：https://docs.pydantic.dev/dev-v2/usage/types/custom/#adding-validation-and-serialization
"""
from datetime import date, datetime
from typing import Annotated, Any
from pydantic import AfterValidator, PlainSerializer, WithJsonSchema
from .validator import *


# 实现自定义一个手机号类型
Telephone = Annotated[
    str,
    AfterValidator(valid_telephone),
    PlainSerializer(lambda x: x, return_type=str),
    WithJsonSchema({'type': 'string'}, mode='serialization')
]

# 实现自定义一个邮箱类型
Email = Annotated[
    str,
    AfterValidator(valid_email),
    PlainSerializer(lambda x: x, return_type=str),
    WithJsonSchema({'type': 'string'}, mode='serialization')
]

# 实现自定义一个日期字符串的数据类型
DateStr = Annotated[
    str | date | int | float,
    AfterValidator(valid_date),
    PlainSerializer(lambda x: x, return_type=str),
    WithJsonSchema({'type': 'string'}, mode='serialization')
]

# 实现自定义一个日期时间字符串的数据类型
DatetimeStr = Annotated[
    str | datetime | int | float | dict,
    AfterValidator(valid_datetime),
    PlainSerializer(lambda x: x, return_type=str),
    WithJsonSchema({'type': 'string'}, mode='serialization')
]
